﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace XCommunity.Documents
{
  public class TextDocument : Document
  {


    private string _content;


    public TextDocument( string content, DocumentMeta meta )
      : base( meta )
    {
      _content = content;
      _loaded = true;
    }

    public TextDocument( DocumentMeta meta )
      : base( meta )
    {
      _loaded = false;
    }



    public override void SaveDocument( Stream stream )
    {
      using ( StreamWriter writer = new StreamWriter( stream, Encoding.UTF8 ) )
      {
        writer.Write( _content );
      }
    }

    public override void LoadDocument( Stream stream )
    {
      if ( Loaded )
        throw new InvalidOperationException();

      using ( StreamReader reader = new StreamReader( stream, Encoding.UTF8 ) )
      {
        _content = reader.ReadToEnd();
      }

      _loaded = true;
    }

    private bool _loaded;
    public override bool Loaded
    {
      get
      {
        return _loaded;
      }
    }

    public string GetContent()
    {
      if ( !Loaded )
        throw new InvalidOperationException();

      return _content;
    }
  }
}
